package jee.boot.module.mail.config;

import jee.boot.module.mail.interceptor.DefaultMailPostInterceptor;
import jee.boot.module.mail.interceptor.DefaultMailPreInterceptor;
import jee.boot.module.mail.sender.DefaultMailSender;
import jee.boot.module.mail.sender.DefaultMailTaskHandler;
import jee.boot.module.mail.sender.MailTaskHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

@Configuration
public class MailConfig {
    @Autowired
    private DefaultMailPreInterceptor defaultMailPreInterceptor;
    @Autowired
    private DefaultMailPostInterceptor defaultMailPostInterceptor;
    @Autowired
    private DefaultMailSender sender;

    @Bean
    public MailTaskHandler mailTaskHandler(){
        DefaultMailTaskHandler defaultMailTaskHandler=new DefaultMailTaskHandler();
        defaultMailTaskHandler
                .addPreInterceptor(defaultMailPreInterceptor)//增加前置处理器,用于预处理邮件数据
                .addPostInterceptor(defaultMailPostInterceptor)// 增加后置处理器,用于存储邮件
                .setSender(sender);
        return defaultMailTaskHandler;
    }
    @Bean
    public FreeMarkerConfigurer  freeMarkerConfigurer(@Autowired DatabaseTemplateLoader databaseTemplateLoader){
        FreeMarkerConfigurer config=new FreeMarkerConfigurer();
        config.setPostTemplateLoaders(databaseTemplateLoader);
        return config;
    }
}
